package com.egu.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.egu.common.util.DateUtil;
import com.egu.common.util.UserUtil;
import com.egu.mapper.DepartmentInfoMapper;
import com.egu.mapper.UserMapper;
import com.egu.mapper.UserRoleMapper;
import com.egu.model.DepartmentInfo;
import com.egu.model.User;
import com.egu.model.UserExample;
import com.egu.model.UserExample.Criteria;
import com.egu.model.UserRole;
import com.egu.service.UserService;
import com.egu.vo.PageParam;
import com.egu.vo.RequestPageVo;
import com.egu.vo.RequestResultVo;

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserMapper userMapper;

	@Autowired DepartmentInfoMapper departmentInfoMapper;
	
	@Autowired UserRoleMapper userRoleMapper;
	
	
	public User findUserByUserName(String userName) {
		UserExample example = new UserExample();
		example.createCriteria().andUserNumEqualTo(userName).andIsUsedEqualTo("1");
		List<User> userList = userMapper.selectByExample(example);
		if(userList.size()>0) {
			return userList.get(0);
		}
		return null;
	}

	
	
	public RequestPageVo<User> getList(PageParam param, User user) {
		RequestPageVo<User> result = new RequestPageVo<User>();
		UserExample example = new UserExample();
		Criteria criteria = example.createCriteria();
		criteria.andIsUsedEqualTo("1");
		if (StringUtils.isNotBlank(user.getUserName())) {
			criteria.andUserNameLike("%"+user.getUserName()+"%");
		}
		int count = (int) userMapper.countByExample(example);
		if (count > 0) {
			example.setStartPage(param.getOffset());
			example.setPageSize(param.getLimit());
			List<User> list = userMapper.selectByExample(example);
			
			for (User user2 : list) {
				DepartmentInfo departmentInfo = departmentInfoMapper.selectByPrimaryKey(user2.getDepartmentId());
				UserRole userRole = userRoleMapper.selectByPrimaryKey(user2.getRoleId());
				user2.setDepartmentName(departmentInfo.getDepartmentName());
				user2.setRoleName(userRole.getRoleName());
			}
			
			result.setRows(list);
			result.setTotal(count);
		}
		return result;
	}

	
	
	
	
	
	@Override
	public RequestResultVo<List<User>> getAllList() {
		UserExample example = new UserExample();
		example.createCriteria().andIsUsedEqualTo("1");
		List<User> list = userMapper.selectByExample(example);
		
		ArrayList<User> arrayList = new ArrayList<User>();
		//+++++
		for (User user : list) {
			Integer roleId = user.getRoleId();
			UserRole selectByPrimaryKey = userRoleMapper.selectByPrimaryKey(roleId);
			if(selectByPrimaryKey.getRolePower()==1) {
				arrayList.add(user);
			}
		}
		
		return RequestResultVo.success(arrayList);
	}

	@Override
	@Transactional
	public RequestResultVo<User> saveOrUpdate(User user) {
		
		User currentUser = UserUtil.getCurrentUser();
		if (user.getId() == null) {
			user.setUserAddman(currentUser.getUserName());
			user.setUserAddtime(DateUtil.getCurrentDate());
			user.setUserPw(DigestUtils.md5Hex(user.getUserPw()));
			userMapper.insertSelective(user);
		} else {
			user.setUserChangeman(currentUser.getUserName());
			user.setUserChangetime(DateUtil.getCurrentDate());
			userMapper.updateByPrimaryKeySelective(user);
		}
		return RequestResultVo.success();
	}

	@Override
	@Transactional
	public RequestResultVo<User> deleteDataById(Integer id) {
		User currentUser = UserUtil.getCurrentUser();
		User user = new User();
		user.setIsUsed("0");
		user.setId(id);
		user.setUserChangeman(currentUser.getUserName());
		user.setUserChangetime(DateUtil.getCurrentDate());
		try {
			userMapper.updateByPrimaryKeySelective(user);
		} catch (Exception e) {
			return RequestResultVo.error("数据库异常");
		}
		return RequestResultVo.success();
	}


	@Transactional
	public boolean savePw(String userPw, String newUserPw) {
		User currentUser = UserUtil.getCurrentUser();
		User user = userMapper.selectByPrimaryKey(currentUser.getId());
		String oldUserPw = user.getUserPw();
		if(DigestUtils.md5Hex(userPw).equals(oldUserPw)) {
			user.setUserPw(DigestUtils.md5Hex(newUserPw));
			userMapper.updateByPrimaryKeySelective(user);
			return true;
		}
		
		return false;
	}
}
